package com.sharker.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.sharker.R;
import com.sharker.adapter.CourseExamAdapter;
import com.sharker.application.App;
import com.sharker.models.courseexam.CourseExamBean;
import com.sharker.models.courseexam.ExamCommit;
import com.sharker.models.courseexam.Question;
import com.sharker.network.SharkerParams;
import com.sharker.view.Toptitlebar;

import org.xutils.common.Callback;
import org.xutils.x;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 课后测试界面
 */
public class ReviewTestActivity extends BaseActivity{

    @BindView(R.id.courseExamTitleBar)
    Toptitlebar mCourseExamTitleBar;
    @BindView(R.id.zyCourseName)
    TextView zyCourseName;
    @BindView(R.id.zyCourseState)
    TextView zyCourseState;
    @BindView(R.id.review_test_recy)
    RecyclerView review_test_recy;
    private String answer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review_test);
        //绑定activity
        ButterKnife.bind(this);
        getTestData();
        mCourseExamTitleBar.setleftOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 获取课后测试数据
     */
    private void getTestData() {
        SharkerParams sharkerParams = new SharkerParams("exam_course");
        sharkerParams.addBodyParameter("session", "001");
        sharkerParams.addBodyParameter("object_id", "10001");
        x.http().post(sharkerParams, new Callback.CommonCallback<CourseExamBean>() {
            @Override
            public void onSuccess(CourseExamBean result) {
                if(result != null){
                    String title = result.title;
                    String note = result.note;
                    List<Question> questionContent = result.questionContent;
                    ExamData(title,note,questionContent);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                commitBtn();
            }
        });
    }
    //适配数据
    private void ExamData(String title, String note, List<Question> question) {
        zyCourseName.setText(title);
        zyCourseState.setText(note);
        review_test_recy.setLayoutManager(new LinearLayoutManager(App.getInstance()));
        CourseExamAdapter courseExamAdapter =  new CourseExamAdapter(this,question);
        review_test_recy.setAdapter(courseExamAdapter);
        answer = courseExamAdapter.getTestResult();
    }
    /**
     * 提交测试按钮
     */
    @OnClick(R.id.review_TvCommit)
    void commitBtn(){
        commitAnswer();
    }

    /**
     * 提交课后测试答卷
     */
    private void commitAnswer(){
        SharkerParams params = new SharkerParams("exam_submit");
        params.addBodyParameter("session","");
        params.addBodyParameter("object_id","");
        params.addBodyParameter("answer",answer);
        x.http().post(params, new Callback.CommonCallback<ExamCommit>() {
            @Override
            public void onSuccess(ExamCommit result) {
                int exam_score = result.exam_score;
                Intent  intent = new Intent(ReviewTestActivity.this,ShowResultActivity.class);
                intent.putExtra("score",exam_score);
                startActivity(intent);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
